using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JinianNet.JNTemplate;
using Xunit;
using YTS.Tools;
using System.Collections;

namespace LanguageLearnTestCase.DataType
{
    public class TestIsList
    {
        public class Order
        {
            public string OrderNo { get; set; }
            public decimal Money { get; set; }
            public IList<Product> DtList { get; set; }
        }
        public class Product
        {
            public string Name { get; set; }
            public decimal Price { get; set; }
            public int Number { get; set; }
        }
        private Order GetTestCaseOrder()
        {
            Order order = new Order()
            {
                OrderNo = "5674687687",
                Money = 8462.51M,
                DtList = new List<Product>() {
                    new Product() {
                        Name = "产1",
                        Price = 413.4M,
                        Number = 4,
                    },
                    new Product() {
                        Name = "产2",
                        Price = 843.51M,
                        Number = 3,
                    },
                    new Product() {
                        Name = "产3",
                        Price = 1487.44M,
                        Number = 6,
                    },
                },
            };
            return order;
        }

        [Fact]
        public void IsHaveList()
        {
            Order order = GetTestCaseOrder();
            Type orderType = order.GetType();

            bool IsListProperty = false;
            PropertyInfo[] propArr = orderType.GetProperties();
            foreach (PropertyInfo prop in propArr)
            {
                var propValue = prop.GetValue(order, null);
                Type dataType = prop.PropertyType;
                if (dataType.IsGenericType)//判断是否是泛型
                {
                    Type[] genericTypes = dataType.GetGenericArguments();
                    foreach (Type t in genericTypes)
                    {
                        Console.WriteLine(t.Name);
                    }
                }

                //想判断这个list的类型 并遍历其中所有元素 怎么做?
                if (IsIEnumerable(dataType))
                {
                    IsListProperty = true;
                    IEnumerable en = propValue as IEnumerable;
                    foreach (object obj in en)
                        Console.WriteLine(obj);
                }
            }
            Assert.True(IsListProperty);
        }

        public static bool IsIEnumerable(Type type)
        {
            var interfaceList = type.GetInterfaces();
            return Array.IndexOf(interfaceList, typeof(IEnumerable)) > -1;
        }
    }
}
